package com.hzhk.happyrent.ui.activity.rent;

import android.app.Activity;
import android.graphics.Bitmap;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.HttpAuthHandler;
import android.webkit.SslErrorHandler;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hzhk.happyrent.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoadWebViewActivity extends Activity {

    @BindView(R.id.webView)
    WebView webView;
    @BindView(R.id.activity_titlebar_left)
    ImageView activityTitlebarLeft;
    @BindView(R.id.activity_titlebar_center)
    TextView activityTitlebarCenter;
    @BindView(R.id.activity_titlebar_right)
    ImageView activityTitlebarRight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_load_web_view);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {

        activityTitlebarLeft.setImageResource(R.drawable.back);
        activityTitlebarRight.setVisibility(View.GONE);
        activityTitlebarCenter.setText(getIntent().getStringExtra("name"));
        //实例化WebView对象
        //设置WebView属性，能够执行Javascript脚本
        webView.getSettings().setJavaScriptEnabled(true);
        //设置可自由缩放网页
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setBuiltInZoomControls(true);
//        webView.getSettings().setLoadWithOverviewMode(true);


        webView.setWebViewClient(new WebViewClient() {


            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);

                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if(!TextUtils.isEmpty(url)&&url.contains("http://zzl.paituwang.com/zapis/zmxy/index")){
//                    Toast.makeText(LoadWebViewActivity.this, url, Toast.LENGTH_SHORT).show();
                    new Handler(new Handler.Callback() {
                        @Override
                        public boolean handleMessage(Message msg) {
                            if(1==msg.what){
                                setResult(1);
                                finish();
                            }
                            return false;
                        }
                    }).sendEmptyMessageDelayed(1,1000);
                }
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
//                view.loadUrl(url);
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onReceivedSslError(WebView view,
                                           SslErrorHandler handler, SslError error) {
                handler.proceed();
            }

            @Override
            public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
//                Toast.makeText(LoadWebViewActivity.this, host, Toast.LENGTH_SHORT).show();
                super.onReceivedHttpAuthRequest(view, handler, host, realm);
            }

        });

        //加载需要显示的网页
        webView.loadUrl(getIntent().getStringExtra("uil"));
//        webView.loadUrl("file:///android_asset/xieyi.html");


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    @OnClick(R.id.activity_titlebar_left)
    public void onViewClicked() {
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}